<?php defined('SYSPATH') or die('No direct script access.');
	
	class Model_Services_Pathology extends Model implements Jqgrid_GridListener{
		
		public $pathology = NULL;
		
		public function count($criteria = false){
			return ORM::factory("dao_pathology")->count_all();
		}
			
		public function getList($criteria = false){
			
			if (!$criteria)	$pathology_list = ORM::factory("dao_pathology")->find_all()->as_array();
			else{
				if ($criteria->isSearch){
					$pathology_list = ORM::factory("dao_pathology")
									->where($criteria->getSQLField("pathologies"),$criteria->getSQLOper(), $criteria->getSQLSearchString())
									->order_by($criteria->sidx,$criteria->sord)
									->limit($criteria->limit)
									->offset($criteria->calcOffset())
									->find_all()
									->as_array();
							
				}else{
					$pathology_list = ORM::factory("dao_pathology")->order_by($criteria->sidx,$criteria->sord)->limit($criteria->limit)->offset($criteria->calcOffset())->find_all()->as_array(); 
				}
			} 
		
			$dataset = array();
			foreach ($pathology_list as $pathology){
				$dataset[$pathology->id] = array($pathology->pathology_name,$pathology->initials, $pathology->description);
			}
			
			return $dataset;
		}
	
		/**
		*
		* get all pathologies from an id
		* @param int $minId
		* @param int $maxAmount
		* @return array of pathologies
		*/
		public function getListFromMinID($minId, $maxAmount){
			return ORM::factory("dao_pathology")->where("id",">",$minId)->order_by("id", "ASC")->limit($maxAmount)->find_all()->as_array();
		}
		
		public function get($id){
			return ORM::factory("dao_pathology")->find($id);
		}
		
		public function getAll(){ return ORM::factory("dao_pathology")->find_all()->as_array();}
		
		public function save($id, array $data){
			$this->pathology = ($id > 0) ? ORM::factory("dao_pathology",$id) : ORM::factory("dao_pathology");
			$this->pathology->pathology_name = $data['pathology_name'];
			$this->pathology->initials = $data['initials'];
			$this->pathology->description = $data['description'];
			$this->pathology->save();
			return $this->pathology->id;
	    }
		
		public function delete($id){
			$pathology = ORM::factory("dao_pathology",$id);
			$pathology->delete();
			return $id;
		}
		
		
		/**
		*
		* return an entity pathology by name
		* @param string $name
		* @return pathology
		*/
		public function getByName($name){
			return ORM::factory("dao_pathology")->where("pathology_name","=",$name)->find();
		}
		
	}
	
	
	
	
?>
